<template>
  <div>
    <el-card>
      <el-row type="flex" justify="space-around">
        <el-col :span="6"><el-button type="primary" @click="addAgent">添加经纪人</el-button></el-col>
        <el-col><el-input v-model="content" placeholder="请输入内容" /></el-col>
        <el-col
          :span="10"
        ><el-button icon="el-icon-search" @click="search">搜索</el-button></el-col>
      </el-row>
      <el-table :data="tableData" border style="width: 100%" class="table">
        <el-table-column prop="image" label="图片" width="180">
          <template v-slot="{row}">
            <img :src="row.image" alt="" width="100%">
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="desc" label="介绍" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        background
        :page-size="limit"
        layout="prev, pager, next"
        :total="total"
        :current-page="currentPage"
        @current-change="changePage"
      />
    </el-card>
    <AddEco
      ref="addEco"
      :show-dialog.sync="visibleDialog"
      @updateEvent="getEconomicList"
    />
  </div>
</template>

<script>
import AddEco from './components/add-eco.vue'
import { getEconomic, delEconomic, getEconomicNum } from '@/api/brokerage'
export default {
  components: {
    AddEco
  },
  data() {
    return {
      tableData: [
        { name: '',
          image: '',
          email: '',
          phone: '',
          address: '',
          desc: ''
        }
      ],
      visibleDialog: false,
      total: 0,
      start: 0,
      limit: 5,
      page: 0,
      content: '',
      currentPage: 1
    }
  },
  created() {
    // 刷新列表
    this.getEconomicList()
  },
  methods: {
    //  封装渲染
    async getEconomicList() {
      const res = await getEconomic({
        _start: this.start,
        _limit: this.limit,
        name_contains: ''
      })
      const list = await getEconomicNum()
      this.tableData = res.data
      console.log(list, 86)
      this.total = list.data
    },
    addAgent() {
      this.visibleDialog = true
    },
    // 根据id回去经纪详情
    editFn(id) {
      this.visibleDialog = true
      this.$refs.addEco.getEconomic(id)
    },
    // 删除
    async delFn(id) {
      this.$confirm('是否删除角色？', '删除', {
        type: 'warning'
      }).then(async() => {
        // 调用接口
        await delEconomic(id)

        // 刷新列表
        this.getEconomicList()
      }).cath(() => {
        console.log('点击了取消按钮')
      })
    },
    changePage(curPage) {
      console.log(curPage)
      this.start = curPage === 1 ? 0 : (curPage - 1) * this.limit
      this.getEconomicList()
    },
    // 模糊搜索
    async  search() {
      const res = await getEconomic({
        _start: this.start,
        _limit: this.limit,
        name_contains: this.content
      })
      this.tableData = res.data
      this.content = ''
    }

  }

}
</script>

<style>
.table{
  margin-top: 20px;
}
  </style>
